package com.runda.partypolicy.app.base;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import com.blankj.utilcode.util.ActivityUtils;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.runda.partypolicy.R;
import com.runda.partypolicy.app.ApplicationMine;
import com.runda.partypolicy.app.di.AppViewModelFactory;
import com.runda.partypolicy.utils.SystemBarHelper;

import javax.inject.Inject;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dagger.android.AndroidInjection;


/**
 * author:  RD_YH
 * date:    2018/10/9
 * version: v1.0
 * description: 基础FragmentActivity
 */
public abstract class BaseFragmentActivity<T extends BaseViewModel> extends AppCompatActivity {

    @Inject
    AppViewModelFactory viewModelFactory;
    private T viewModel;
    protected Unbinder unBinder;

    private KProgressHUD waitingView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        ActivityUtils.getActivityList().add(this);
        setContentView(getLayout());
        setupStatusBar();
        unBinder = ButterKnife.bind(this);
        viewModel = initViewModel();
        setupFragments();
        initEvents();
        initNotSignEvent();
        initNoNetworkEvent();
        initShowMessageEvent();
        initStateLayoutEvent();
        initTokenOverTimeEvent();
        initShowOrDismissWaitingEvent();
        start();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ApplicationMine.getInstance().getRefWatcher().watch(this);
        if (unBinder != null) {
            unBinder.unbind();
        }
        ActivityUtils.getActivityList().remove(this);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    private void setupStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            SystemBarHelper.immersiveStatusBar(this, 0.0001f);
        }
        SystemBarHelper.setStatusBarDarkMode(this);
    }

    public KProgressHUD getWaitingView(boolean cancelable, String message) {
        hideWaitingView();
        waitingView = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(cancelable)
                .setLabel(getString(R.string.pleaseWaiting))
                .setDetailsLabel(message)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f);
        return waitingView;
    }

    public void hideWaitingView() {
        if (waitingView != null && waitingView.isShowing()) {
            waitingView.dismiss();
        }
    }
//
//    public Alerter getNotSignedAlerter() {
//        return Alerter.create(this)
//                .setDuration(10000)
//                .enableSwipeToDismiss()
//                .setTitle(R.string.notSigned)
//                .setText(R.string.notSigned_content)
//                .setBackgroundColorRes(R.color.baseColor)
//                .setOnClickListener(view -> {
//                    Alerter.hide();
////                    ActivityUtils.startActivity(this, ActivityLogin.class);
//                });
//    }
//
//    public Alerter getNoNetworkAlerter() {
//        return Alerter.create(this)
//                .setDuration(10000)
//                .enableSwipeToDismiss()
//                .setTitle(R.string.noNetwork)
//                .setText(R.string.noNetwork_content)
//                .setBackgroundColorRes(R.color.baseColor)
//                .setOnClickListener(view -> {
//                    Alerter.hide();
//                    startActivity(IntentUtils.getLaunchAppDetailsSettingsIntent(
//                            AppUtils.getAppPackageName(), true));
//                });
//    }
//
//    public Alerter getTokenOvertimeAlerter() {
//        return Alerter.create(this)
//                .setDuration(10000)
//                .enableSwipeToDismiss()
//                .setTitle(R.string.tokenOvertime)
//                .setText(R.string.tokenOvertime_content)
//                .setBackgroundColorRes(R.color.baseColor)
//                .setOnClickListener(view -> {
//                    Alerter.hide();
////                    ActivityUtils.startActivity(this, ActivityLogin.class);
//                });
//    }

    public T getViewModel() {
        return viewModel;
    }

    public AppViewModelFactory getViewModelFactory() {
        return viewModelFactory;
    }

    public abstract int getLayout();

    public abstract T initViewModel();

    public abstract void setupFragments();

    public abstract void initEvents();

    public abstract void start();

    public abstract void initNotSignEvent();

    public abstract void initNoNetworkEvent();

    public abstract void initShowMessageEvent();

    public abstract void initTokenOverTimeEvent();

    public abstract void initShowOrDismissWaitingEvent();

    public abstract void initStateLayoutEvent();
}
